<?php

/**
 * Created by PhpStorm.
 * User: holy
 * Date: 03/11/2016
 * Time: 18:35
 */

namespace app\modules\groups\models;

use Yii;

class TreeForm extends \yii\db\ActiveRecord {

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%group_tree}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['name', 'lft'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'name' => Yii::t('app', 'groupID'),
            'lft' => Yii::t('app', 'groupName'),
            'rgt' => Yii::t('app', '组织代码'),
            'lvl' => Yii::t('app', '组织代码'),
        ];
    }

    /**
     * 组织结构第一级别
     * @param type $model
     * 1.更改数组，
     * 2.更改root---id
     */
    public static function saveOne() {
//        $array = [ 
//            '一公司','二公司','三公司','五公司','六公司','七公司','八公司','九公司','10公司','11公司','12公司','15公司','十六公司','十七公司','十八分公司','直属营销3中心','金泰职能','十公司','电销中心','直属营销1中心','直属营销2中心','直属营销四中心','13公司','樊喜管理部','北分职能','大客户业务部','19公司','昭名公司','基金项目运营部',
//         ];
        $array = [
            '金泰',
            '华东区',
            '作业管理部',
            '其它',
        ];


        foreach ($array as $value) {

            $_tree = TreeForm::find()->where(['id' => 86])->one();
            $tree_all = TreeForm::find()->where(['root' => $_tree->id])->orderBy('id DESC')->one();
//            if (count($tree_all) > 0) {//保存二级分类   
            $trees = new TreeForm();
            $trees->name = $value;
            $trees->root = $_tree->id;


            $trees->lft = $tree_all->rgt + 1;
            $trees->rgt = $trees->lft + 1;
            $trees->lvl = 1;
            $trees->save();
            //改变自己的root and  rgt;
            $_tree->root = $_tree->id;
            $_tree->rgt = $trees->rgt + 1;
            $_tree->save();
        }
        return true;
    }

    /**
     * 组织结构第二级别
     * @param type $model
     * 修改下面4步骤
     */
    public static function saveTow() {
        //这个每次都改
        $array = [ 
          '总部','职能部门','11无效公司','四公司','互联中心','王珍营业部','世奥中心','恒信线上','恒信特殊'
         ];
        //1.查询到最后的一级ID//86///87//lef-rgt 数最大的
        $_treeer = TreeForm::find()->where(['id' => 1417])->one(); //获取最后一级的rgt 获取最后一级的
        //2.修改成自己的ID
        $_treeoness = TreeForm::find()->where(['id' => 1418])->one(); //获取最后一级的rgt 自己的
        $_treeoness->lft = $_treeer->rgt + 1;
        $_treeoness->rgt = $_treeoness->lft + 1;
        $_treeoness->root =$_treeoness->id;
        $_treeoness->save();
        
        foreach ($array as $value) {
            //3.修改成自己的ID
              $_tree = TreeForm::find()->where(['id' => 1418])->one();//和上面$_treeoness-》id 一样的值
            $tree_all = TreeForm::find()->where(['root' => $_tree->id])->orderBy('id DESC')->one();

            $trees = new TreeForm();
            $trees->name = $value;
            $trees->root = $_tree->id;


            $trees->lft = $tree_all->rgt + 1;
            $trees->rgt = $trees->lft + 1;
            //4.修改级别，每级加1。
            $trees->lvl = 2; //每一级都要更改//2//3///4///5
            $trees->save();
            //改变自己的root and  rgt;
            $_tree->root = $_tree->id;
            $_tree->rgt = $trees->rgt + 1;
            $_tree->save();
            
        }
        return true;
    }

    public static function getDates($id) {
        $tree = self::findOne($id);
        if (isset($tree)) {
            return $tree->name;
        } else {
            return '';
        }
    }

//     public static function saveShop($model) {
//        $_tree = TreeForm::find()->where(['source_table_pk' => $model->bdID])->one();
//        $tree_all = TreeForm::find()->where(['root' => $_tree->id])->orderBy('id DESC')->one();
//        if (count($_tree) > 0) {//保存二级分类
//            $trees = new TreeForm();
//            $trees->name = $model->shop_name;
//            $trees->root = $_tree->id;
//            if (count($tree_all) > 0) {
//                $trees->lft = $tree_all->rgt + 1;
//                $trees->rgt = $trees->lft + 1;
//            } else {
//                $trees->lft = $_tree->lft + 1;
//                $trees->rgt = $trees->lft + 1;
//            }
//            $trees->lvl = 1;
//            $trees->source_table = 'zyf_shop';
//            $trees->source_table_pk = $model->bdID;
//            $trees->save();
//            //改变自己的root and  rgt;
//            $_tree->root = $_tree->id;
//            $_tree->rgt = $trees->rgt + 1;
//            $_tree->save();
//        }
//    }
//    public static function getDates($id) {
//        $tree = self::findOne($id);
//        if (isset($tree)) {
//            return $tree->name;
//        } else {
//            return '';
//        }
//    }
}
